<?php

namespace app\hotel\controller;

use think\Controller;

class Coupon extends Controller
{
    public function __construct(){
        parent::__construct();

        $this->BusinessModel = model('Business.User');
        $this->CouponModel = model('Hotel.Coupon');
        $this->CouponListModel = model('Hotel.Couponlist');
    }

    // 首页优惠券列表
    public function list(){
        if($this->request->isPost()){
            $result = $this->CouponModel
                    ->where(['status'=>['IN',[0,1]]])
                    ->select();

            if($result === FALSE){
                $this->error('暂无优惠券信息');
                exit;
            }else{
                $this->success('返回优惠券',null,$result);
                exit;
            }
        }
    }

    
    // 优惠券详情
    public function info(){
        if($this->request->isPost()){
            $id = $this->request->param('id',0,'trim');
            $busid = $this->request->param('busid',0,'trim');

            // 查询优惠券
            $coupon = $this->CouponModel->find($id);
            if(!$coupon){
                $this->error('未知信息');
                exit;
            }

            $business = $this->BusinessModel->find($busid);
            if($business){
                // 登录了就去添加一个领取字段,并且查找是否领取做出判断
                $check = $this->CouponListModel->where(['cid' => $id, 'busid' => $busid])->find();

                $coupon['receive'] = $check ? true : false;

            }else{
                // 没登录就去添加一个领取字段为false
                $coupon['receive'] = false;
            }

            // 查找领取表
            $receive = $this->CouponListModel->with(['business'])->where(['cid' => $id])->select();

            $data = [
                'coupon' => $coupon,
                'receive' => $receive
            ];

            $this->success('返回信息',null,$data);
            exit;
        }
    }


    // 领取优惠券
    public function receive(){
        if($this->request->isPost()){
            $cid = $this->request->param('cid',0,'trim');
            $busid = $this->request->param('busid',0,'trim');

            // 查找优惠券
            $coupon = $this->CouponModel->find($cid);
            if(!$coupon){
                $this->error('暂无优惠券信息');
                exit;
            }

            if($coupon['status'] == 2){
                $this->error('活动已结束');
                exit;
            }

            if($coupon['status'] == 0){
                $this->error('活动还未开始');
                exit;
            }

            if($coupon['stock'] <= 0){
                $this->error('优惠券已全部领取完毕');
                exit;
            }

            // 查找用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 查看用户是否领取
            $check = $this->CouponListModel->where(['cid'=>$cid, 'busid'=>$busid])->find();

            if($check){
                $this->error('您已领取过该优惠券，无法再次领取');
                exit;
            }

            // 开启事务
            $this->CouponModel->startTrans();
            $this->CouponListModel->startTrans();

            $CouponListData = [
                'cid' => $cid,
                'busid' => $busid,
                'status' => 1
            ];

            $CouponListStatus = $this->CouponListModel->save($CouponListData);
            if($CouponListStatus === FALSE){
                $this->error($this->CouponListModel->getError());
                exit;
            }

            $stock = bcsub($coupon['stock'],1);
            $stock = $stock <= 0 ? 0 : $stock;

            $CouponData = [
                'id' => $cid,
                'stock' => $stock
            ];

            $CouponStatus = $this->CouponModel->isUpdate(true)->save($CouponData);

            if($CouponStatus === FALSE){
                $this->CouponListModel->rollback();
                $this->error($this->CouponModel->getError());
                exit;
            }

            if($CouponListStatus === FALSE || $CouponStatus === FALSE){
                $this->CouponModel->rollback();
                $this->CouponListModel->rollback();
                $this->error('领取失败，请重试');
                exit;
            }else{
                $this->CouponModel->commit();
                $this->CouponListModel->commit();
                $this->success('领取成功');
                exit;
            }
        }
    }


    // 我的优惠券列表
    public function couponlist(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $status = $this->request->param('status',0,'trim');

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户信息未知');
                exit;
            }

            $where = [
                'busid' => $busid
            ];

            if($status != 0){
                $where['couponlist.status'] = $status;
            }

            $list = $this->CouponListModel
                ->with(['coupon'])
                ->where($where)
                ->order('id desc')
                ->select();
            
            if(!$list){
                $this->error('暂无更多优惠券');
                exit;
            }else{
                $this->success('返回优惠券',null,$list);
                exit;
            }
        }
    }

}
